<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%!
	public Message addTemplate(Integer id, Integer uid) {
		List<Map> templates = QBuilder.db("basedb").select("new map(template_name as template_name, template_type as template_type, src_id as src_id, is_active as is_active, author as author, status as status, json as json, uid as uid)").from("visit_template").where(new Where("id =", id).and("status = ", STATUS_ENABLE)).query().getResultObj();
		if (isEmpty(templates)) {
			return new Message(30041, "暂无随访模板");
		}
		List<Map> list = QBuilder.db("basedb").select().from("visit_template").where(new Where("uid=", uid).and("oid=", id).and("status=", STATUS_ENABLE)).query().getResultObj();
		if (!isEmpty(list)) 
			return  new Message(30041, "您已经添加过该模板");
		Map template = templates.get(0);
		Integer authorUid = (Integer)template.get("uid");
		if (uid.equals(authorUid))
			return  new Message(30041, "这份方案已经在您的个人库里了");
		template.put("uid", uid);
		template.put("category", 1);
		template.put("oid", id);
		Integer newId = (Integer)DBTools.db("basedb").addItem("visit_template", template);
		// 2、随访节点
		List<Map> points = QBuilder.db("basedb").select("new map(id as point_id, title as title, show_order as show_order, status as status, json as json, time_difference as time_difference)").from("visit_point").where(new Where("template_id =", id).and("status = ", STATUS_ENABLE)).order("show_order", QBuilder.asc).query().getResultObj();
		
		if(!isEmpty(points)) {
			// 3、节点下的关联
			String pointIds = "-1";
			Map<Integer, Integer> idMap = new HashMap<>();
			for (int i = 0; i < points.size(); i++) {
				Map point = points.get(i);
				point.put("template_id", newId);
				Integer oldId = (Integer)point.remove("point_id");
				pointIds += "," + Tools.getDesc(oldId, "-1");
				idMap.put(oldId, (Integer)DBTools.db("basedb").addItem("visit_point", point));
			}
			List<Map> links = QBuilder.db("basedb").select("new map(link_type as link_type, link_id as link_id, status as status, link_order as link_order, json as json, point_id as point_id)").from("visit_point_link").where(new Where("status = ", STATUS_ENABLE).and("point_id in (" + pointIds + ")")).query().getResultObj();
			if (!isEmpty(links)) {
				for (int i = 0; i < links.size(); i++) {
					Map link = links.get(i);
					link.put("point_id", idMap.get((Integer)link.get("point_id")));
				}
				DBTools.db("basedb").addItem("visit_point_link", links);
			}
		}
		return new Message(0, "添加成功");
	}
%>
<%
	// 添加为自己的模板
	String templateId = input.getDesc("template_id", "-1");
	Integer uid = token.getUid();
	User doctor = userByUid(uid);
	if (doctor == null || !USER_EMPLOYEE.equals(doctor.getType()) || Tools.getDesc(doctor.getJson("role")).equals("2")) {
		msg = new Message(30041, "您没有权限");
		Tools.encryptPrint(out, msg);
        return;
	}
	List<String> templateList = stringToList(templateId);
	for (String id:templateList) {
		msg = addTemplate(Tools.getInt(id), uid);
	}
	if (templateList.size() != 1) 
		msg = new Message(0, "添加成功");
	Tools.encryptPrint(out, msg);
%>